//
//  HomeOfficialRecommendCell.swift
//  HWanGame
//
//  Created by 王昌健 on 2021/12/24.
//  Copyright © 2021 94Wan. All rights reserved.
//

import UIKit
import Cosmos
class HomeOfficialRecommendCell: UITableViewCell {
    
    var collectionView:UICollectionView!
    let collectionProtocol = HWOfficialCellProtocol()
    
    var models:[HWHomeGameModel]? {
        didSet {
            collectionProtocol.models = models
            collectionView.reloadData()
        }
    }
    
    var didClickOfficialGame:((_:HWHomeGameModel)->())?
    
    class func cellWithTableView(tableView:UITableView) -> (HomeOfficialRecommendCell) {
        let reUse:String = "HomeOfficialRecommendCell"
    
        var cell = tableView.dequeueReusableCell(withIdentifier: reUse)
        
        if cell == nil {
            cell = HomeOfficialRecommendCell.init(style: UITableViewCell.CellStyle.default, reuseIdentifier: reUse)
        }
        
        return cell as! HomeOfficialRecommendCell
    }
    
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        initUI()
        
        initFrame()
        
        addAction()
    }
    
    func initUI() -> Void {
        selectionStyle = UITableViewCell.SelectionStyle.none
        backgroundColor = UIColor.white
        
        let flowL = UICollectionViewFlowLayout.init()
        flowL.scrollDirection = .horizontal
        collectionView = UICollectionView.init(frame: .zero, collectionViewLayout: flowL)
        collectionView.delegate = collectionProtocol
        collectionView.dataSource = collectionProtocol
        collectionView.backgroundColor = .clear
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.register(HWOfficialGameCell.self, forCellWithReuseIdentifier: "HWOfficialGameCell")
        contentView.addSubview(collectionView)

    }
    
    func initFrame() -> Void {
        collectionView.snp.makeConstraints { make in
            make.top.equalTo(10*widthScale)
            make.left.right.bottom.equalToSuperview().priority(.high)
            make.height.equalTo(200*widthScale)
        }
        
    }
    
    func addAction() -> Void {
        self.collectionProtocol.didClickOfficialGame = {
            self.didClickOfficialGame?($0)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}


class HWOfficialCellProtocol: NSObject {
    var models:[HWHomeGameModel]?
    var didClickOfficialGame:((_:HWHomeGameModel)->())?
}

extension HWOfficialCellProtocol:UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        self.didClickOfficialGame?(models?[indexPath.row] ?? HWHomeGameModel())
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsets(top: 0, left: 13*widthScale, bottom: 0, right: 13*widthScale)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 12*widthScale
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: 109*widthScale, height: 198*widthScale)
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return  models?.count ?? 0
    }
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let gameCell = collectionView.dequeueReusableCell(withReuseIdentifier: "HWOfficialGameCell", for: indexPath) as! HWOfficialGameCell
        
        gameCell.model = models?[indexPath.row]
        return gameCell
        
        
    }
    
}


class HWOfficialGameCell: UICollectionViewCell {
    var bgImageView:UIImageView!
    var alphaView:UIView!
    var nameLabel:UILabel!
    var typeLabel:UILabel!
    var downloadButton:UIButton!
//    var model:HWHomeGuanFangTuiJianModel? {
//        didSet {
//            bgImageView.kf.setImage(with: URL(string: model?.image ?? ""))
//            nameLabel.text = model?.name
//            typeLabel.text = model?.type
//        }
//    }
    var model:HWHomeGameModel? {
        didSet {
            bgImageView.kf.setImage(with: URL(string: model?.image ?? ""))
            nameLabel.text = model?.name
            typeLabel.text = model?.type
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        initUI()
        
        initFrame()
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension HWOfficialGameCell {
    private func initUI() -> Void {
        contentView.backgroundColor = .clear
        
        bgImageView = UIImageView()
        bgImageView.backgroundColor = .clear
        bgImageView.layer.cornerRadius = 18*widthScale
        bgImageView.layer.masksToBounds = true
        bgImageView.contentMode = .scaleAspectFill
        contentView.addSubview(bgImageView)
        
        alphaView = UIView()
        alphaView.frame = CGRect(x: 0, y: 0, width: 109*widthScale, height: 69*widthScale)
        bgImageView.addSubview(alphaView)
        UIColor.gradientColor(view: alphaView, colors: [UIColor.colorWithAlphaHexString(hexString: "#333333", alpha: 0).cgColor,
                                                        UIColor.colorWithAlphaHexString(hexString: "#000000", alpha: 1).cgColor,],start: CGPoint(x: 0, y: 0),end: CGPoint(x: 0, y: 1))

        
        nameLabel = HWLabel.createBoldLabel(title: "", color: "#FFFFFF", font: 13*widthScale)
        nameLabel.textAlignment = .center
        bgImageView.addSubview(nameLabel)
        
        typeLabel = HWLabel.createBoldLabel(title: "", color: "#888888", font: 11*widthScale)
        typeLabel.textAlignment = .center
        bgImageView.addSubview(typeLabel)
        
        downloadButton = UIButton(type: .custom)
        downloadButton.frame = CGRect(x: 0, y: 0, width: 62*widthScale, height: 30*widthScale)
        downloadButton.setTitle("详情", for: .normal)
        downloadButton.titleLabel?.font = UIFont.systemFont(ofSize: 14*widthScale)
        downloadButton.setTitleColor(.colorWithHexString(hexString: "#FA8C08"), for: .normal)
        downloadButton.layer.cornerRadius = 15*widthScale
        downloadButton.layer.masksToBounds = true
        downloadButton.layer.borderColor = UIColor.colorWithHexString(hexString: "#FA8C08").cgColor
        downloadButton.layer.borderWidth = 1
        downloadButton.isEnabled = false
        contentView.addSubview(downloadButton)
        
    }
    
    private func initFrame() -> Void {
        
        bgImageView.snp.makeConstraints { make in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(159*widthScale).priority(.high)
        }
        
        alphaView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.height.equalTo(69*widthScale)
        }
        
        
        nameLabel.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(112*widthScale)
            make.height.equalTo(20*widthScale)
        }
       
        typeLabel.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(nameLabel.snp.bottom)
            make.height.equalTo(20*widthScale)
        }
        
        downloadButton.snp.makeConstraints { make in
            make.top.equalTo(bgImageView.snp.bottom).offset(9*widthScale)
            make.height.equalTo(30*widthScale)
            make.centerX.equalTo(bgImageView)
            make.width.equalTo(62*widthScale)
        }
    }
}

